package kr.co.boogui.actions.news;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import kr.co.boogui.actions.ActionHandler;
import kr.co.boogui.dao.NewsDAO;

/*create table news(
 newsno int auto_increment,
 newstit varchar(50),
 newscontents MEDIUMTEXT,
 wdate timestamp default current_timestamp,
 reades int,
 newstype varchar(2),
 constraint news_pk primary key (newsno)
 );*/

public class NewsAllListAction implements ActionHandler {
	@Override
	public String execute(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {
		// 페이징 변수선언
		int cpage = 1, perpage = 9, startno = 0;
		int newstype=0;
		if(req.getParameter("newstype")!=null){
			newstype=Integer.parseInt(req.getParameter("newstype")); 
		}
		
		// 검색관련 변수
		String ftype = "", fkey = "";

		// cpage, startno 계산
		if (req.getParameter("cpage") != null) {
			cpage = Integer.parseInt(req.getParameter("cpage"));
			startno = (cpage - 1) * perpage;
		}

		// 총페이지수 계산
		int total = NewsDAO.totalNews(ftype, fkey,newstype);
		int allpage = (int) Math.ceil((float) total / perpage);
		// 네비게이션 시작번호 계산
		int startnav = 0;
		startnav = ((cpage - 1) / 10) * 10 + 1;
		// 글번호 계싼
		int startbno = 0;
		startbno = total - (perpage * (cpage - 1));

		// 검색유형과 검색값처리
		if (req.getParameter("findtype") != null) {
			ftype = req.getParameter("findtype");
		}
		if (req.getParameter("findkey") != null) {
			fkey = req.getParameter("findkey");
		}
		// 뷰로 전달할 데이터 지정

		// 게시판 관련 데이터
		
		req.setAttribute("cpage", cpage);
		req.setAttribute("ftype", ftype);
		req.setAttribute("fkey", fkey);
		req.setAttribute("total", total);
		req.setAttribute("allpage", allpage);
		req.setAttribute("startnav", startnav);
		req.setAttribute("startbno", startbno);
		req.setAttribute("newstype", newstype);
		req.setAttribute("newslist", NewsDAO.All_listNews(startno, ftype, fkey, newstype));
		req.setAttribute("count", NewsDAO.manyNews(newstype));
		
		
		String viewPage = "f|/WEB-INF/jsp/tiles/index.jsp";
		req.setAttribute("action", "../news/news.jsp");
		return viewPage;
	}
}
